package com.example.sirius_swu.myexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sirius_swu.myexample.AppsList.AppManagerAdapter;
import com.example.sirius_swu.myexample.AppsList.AppInfo;
import com.example.sirius_swu.myexample.AppsList.AppInfoService;

/**
 * APP管理的Manager类
 * @author sirius_swu
 */

public class MainActivity extends AppCompatActivity {

    protected static final int SUCCESS_GET_APPLICAITON = 0;

    //布局中的各个控件
    private RelativeLayout rl_loading;
    private ListView lv_appmanage;
    private TextView tv_title;
    //包管理器
    private PackageManager pm;
    //获取手机应用信息的业务类
    private AppInfoService appInfoService;
    //手机应用app信息集合
    private List<AppInfo> appInfos;
    //用户应用程序信息集合
    private List<AppInfo> userAppInfos;
    //是否是所有的app程序,默认为true
    private boolean isAllApp = true;
    //AppManagerAdapter适配器对象
    private AppManagerAdapter mAdapter;

    private PopupWindow mPopupWindow;
    //mHandler方法
    private Handler mHandler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case SUCCESS_GET_APPLICAITON:
                    //给listview去绑定数据，隐藏加载的控件
                    mAdapter = new AppManagerAdapter(getApplicationContext(), userAppInfos);
                    //设置数据
                    lv_appmanage.setAdapter(mAdapter);
                    //隐藏RelativeLayout
                    rl_loading.setVisibility(View.GONE);
                    //View.VISIBLE (控件显示)View.INVISIBLE（控件隐藏  但占据空间）  View.GONE（控件隐藏  不占据空间）
                    break;

                default:
                    break;
            }
        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取布局中的控件
        rl_loading = (RelativeLayout) findViewById(R.id.rl_loading);
        lv_appmanage = (ListView) findViewById(R.id.lv_appmanage);
        tv_title = (TextView) findViewById(R.id.tv_title);
        //实例化AppInfoService对象
        appInfoService = new AppInfoService(this);
        //包管理器
        pm = getPackageManager();

        //在子线程中获取手机安装的应用程序信息
        new Thread(){
            public void run() {
                appInfos = appInfoService.getAppInfos();

                userAppInfos = new ArrayList<AppInfo>();
                for(AppInfo appInfo:appInfos){
                    if(appInfo.isUserApp()){
                        userAppInfos.add(appInfo);
                    }
                }
                Message msg = new Message();
                msg.what = SUCCESS_GET_APPLICAITON;
                mHandler.sendMessage(msg);
            };
        }.start();

        // 安装列表点击事件
        lv_appmanage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String pkn = userAppInfos.get(i).getPackagename();
                long launchCount = userAppInfos.get(i).getaLaunchCount();
                long useTime = userAppInfos.get(i).getaUseTime();
                String netCost = userAppInfos.get(i).getNetCost();
                Toast.makeText(getApplicationContext(), "应用名称："+pkn+";启动次数："+launchCount+";使用时间："+useTime+";网络消耗："+netCost, Toast.LENGTH_LONG).show();
            }
        });
    }

}
